<?php

namespace App\Admin\Controllers\Agents;

use App\Models\Agent;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Hash;
use Dcat\Admin\Http\Controllers\AdminController;

use Illuminate\Support\Facades\DB;
use App\Services\ConfigService;

class AgentController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Agent(), function (Grid $grid) {
            $grid->model()->orderBy('created_at','desc');
            $grid->column('id')->sortable();
            $grid->column('username');
            $grid->column('name');
            // $grid->column('password');
            // $grid->column('balance')->sortable();
            // $grid->column('withdraw')->sortable();
            // $grid->column('rake_back_rate')->sortable();
            $grid->column('withdraw_rate')->sortable();
            $grid->column('deduct')->sortable()->help('表示多少单扣一单');
            $grid->column('state')->using([1 => '正常', 0 => '待审核',-1=>'封禁'])->label([
                'default' => 'primary', // 设置默认颜色，不设置则默认为 default
            
                0 => 'primary',
                -1 => 'danger',
                1 => 'success',
            ]);
            // $grid->column('wechat');
            // $grid->column('phone');
            // $grid->column('parent_id');
            // $grid->column('from_code');
            $grid->column('last_ip');
            $grid->column('created_at')->sortable();
            // $grid->column('updated_at')->sortable();
        
            $grid->enableDialogCreate();
            $grid->disableFilterButton();
            $grid->quickSearch(['id', 'username', 'name','last_ip']);
            // $grid->filter(function (Grid\Filter $filter) {
            //     $filter->equal('id');
        
            // });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Agent(), function (Show $show) {
            $show->field('id');
            $show->field('username');
            $show->field('nickname');
            $show->field('password');
            $show->field('balance');
            $show->field('withdraw');
            $show->field('parent_id');
            $show->field('from_code');
            $show->field('rake_back_rate');
            $show->field('withdraw_rate');
            $show->field('last_ip');
            $show->field('state');
            $show->field('wechat');
            $show->field('phone');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Agent(), function (Form $form) {
            $conf = ConfigService::deductConfig();
            $form->display('id');
            $form->text('username')->required();
            if (!boolval($form->model()->password)) {
                $form->text('password')->default(123456)->saving(function ($v){
                    return Hash::make($v);
                })->required();
            }
            $form->text('name');
            // $form->text('parent_id');
            // $form->text('from_code');
            // $form->rate('rake_back_rate')->default(0.00)->attribute('min', 0)->attribute('max', 100);
            $form->rate('withdraw_rate')->default(30)->attribute('min', 0)->attribute('max', 100);
            $form->text('deduct')->type('number')->attribute('min', 0)->attribute('max', 10000)->default($conf['content']['deduct'])->help('多少单扣一单');
            // $form->text('last_ip');
            $form->radio('state')->options([1 => '启用', 0=> '停用',-1=>'封禁'])->default(1);
            $form->text('wechat');
            $form->text('phone');
        
            $form->text('balance')->default(0.00)->readonly();
            $form->text('withdraw')->default(0.00)->readonly();
            $form->display('created_at');
            $form->display('updated_at');

            $form->saved(function (Form $form, $result) {
                cache()->forget('agent_deduct_'.$form->id);
                // 判断是否是新增操作
                if ($form->isCreating()) {
                    // 也可以这样获取自增ID
                    $newId = $form->getKey();
                    //保存代理角色
                    DB::table(config('agent-admin.database.role_users_table'))->insert(
                        ['agent_id' => $newId, 'role_id' => 1]
                    );
                    return;
                }
            });

            $form->deleted(function (Form $form, $result) {
                // 获取待删除行数据，这里获取的是一个二维数组
                $data = $form->model()->toArray();
                foreach($data as $item){
                    cache()->forget('agent_deduct_'.$item['id']);
                }
            });
        });
    }
}
